﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.Entity;
using System.Diagnostics;

namespace MvcApplication1.Models
{
    public class Pager<T>
    {
        protected int _pagecount;
        protected T currentpage;
        
        public T CurrentPage { get { return currentpage; } }
        public int PageCount { get { return _pagecount; } }

    }

    public class DataViewPager:Pager<DataView>
    {
        public DataViewPager(DataView dv, int pagenumber, int pagesize)    //dv is a sorted dataview
        {
            _pagecount = dv.Count / pagesize;
            if (dv.Count % pagesize != 0) _pagecount++;
            int startindex = (pagenumber - 1) * pagesize;
            currentpage = new DataView(dv.Table.Clone());
            currentpage.AllowNew = true;
            int endindex = startindex + pagesize;
            for (int i = startindex; i < ((endindex > dv.Count) ? dv.Count : endindex); i++)
            {
                DataRowView drv = currentpage.AddNew();
                drv.Row.ItemArray = dv[i].Row.ItemArray;
            }
        }
    }
    public class IQueryableBrandsPager : Pager<IQueryable<Brand>>
    {
        public IQueryableBrandsPager(IQueryable<Brand> brandset, int pagenumber, int pagesize)
        {
            _pagecount = (int)Math.Ceiling((decimal)brandset.Count() / pagesize);
            currentpage=brandset
                .Skip((pagenumber - 1) * pagesize)
                .Take(pagesize);
           
        }
    }

    public class IQueryableUserPager : Pager<IQueryable<User>>
    {
        public IQueryableUserPager(IQueryable<User> userset, int pagenumber, int pagesize)
        {
            _pagecount = (int)Math.Ceiling((decimal)userset.Count() / pagesize);
            currentpage = userset
                .Skip((pagenumber - 1) * pagesize)
                .Take(pagesize);
        }
    }
}